﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QWeiBoN.Util;

namespace QWeiBoN.UserControler
{
    public partial class TopicsControler : UserControl
    {

        #region 私有

        public Action<string> Selected { set; get; }

        /// <summary>
        /// 屏幕尺寸
        /// </summary>
        private Size WindowSize
        {
            get
            {
                return App.Current.RootVisual.RenderSize;
            }
        }

        #endregion

        public TopicsControler()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 显示
        /// </summary>
        public void Show()
        {
            BindData();
            Dispatcher.BeginInvoke(() =>
            {
                TopicsGrid.Width = WindowSize.Width;
                TopicsGrid.Height = WindowSize.Height;
                TopicsConent.Text = string.Empty;
                TopicsPopup.IsOpen = true;
            });
        }

        /// <summary>
        /// 切换显示
        /// </summary>
        public void ToggleShow()
        {
            BindData();
            Dispatcher.BeginInvoke(() =>
            {

                TopicsGrid.Width = WindowSize.Width;
                TopicsGrid.Height = WindowSize.Height;
                TopicsConent.Text = "";
                TopicsPopup.IsOpen = !TopicsPopup.IsOpen;
            });
        }

        /// <summary>
        /// 隐藏
        /// </summary>
        public void Hide()
        {
            BindData();
            Dispatcher.BeginInvoke(() =>
            {
                TopicsPopup.IsOpen = false;
                TopicsConent.Text = string.Empty;
            });
        }

        /// <summary>
        /// 选中或者填入话题的处理
        /// </summary>
        /// <param name="huati"></param>
        private void SelectTopics(string topicsStr)
        {
            if (Selected != null)
            {
                Selected(topicsStr);
            }
        }

        /// <summary>
        /// 绑定数据
        /// </summary>
        public void BindData()
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (TopicsPopup.IsOpen)
                {
                    TopicsList.ItemsSource = TopicsUtil.GetRecentList(20);
                }
            });
        }

        #region 控件事件处理

        /// <summary>
        /// 输入框获得焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TopicsConent_GotFocus(object sender, RoutedEventArgs e)
        {
            TopicsConent.Background = new SolidColorBrush(Color.FromArgb(255, 239, 235, 239));
            TopicsConent.BorderThickness = new Thickness(1);
            TopicsConent.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 148, 150, 148));
        }

        /// <summary>
        /// 输入框失去焦点
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TopicsConent_LostFocus(object sender, RoutedEventArgs e)
        {
            TopicsConent.Background = new SolidColorBrush(Color.FromArgb(255, 239, 239, 239));
            TopicsConent.BorderThickness = new Thickness(1);
            TopicsConent.BorderBrush = new SolidColorBrush(Color.FromArgb(255, 148, 150, 148));
        }

        /// <summary>
        /// 列表tap事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TopicsList_Tap(object sender, GestureEventArgs e)
        {
            var topicsStr = (string)TopicsList.SelectedValue;
            if (!string.IsNullOrWhiteSpace(topicsStr))
            {
                SelectTopics(topicsStr);
            }
        }

        /// <summary>
        /// 插入按钮的事件处理
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void InnerButton_Click(object sender, RoutedEventArgs e)
        {
            var topicsStr = TopicsConent.Text;
            if (!string.IsNullOrWhiteSpace(topicsStr))
            {
                topicsStr = "#" + topicsStr + "#";
                TopicsUtil.SetTopics(topicsStr);
                SelectTopics(topicsStr);
            }
        }

        #endregion

    }
}
